import {
  chineseNumber,
  chineseTen,
  lunarInfo,
  solarTerm,
  termInfo,
} from './CalendarInfo';

// 农历的月日
var parHoliday =
  (month + 1 > 9 ? month + 1 : '0' + (month + 1)) + '' + (i > 9 ? i : '0' + i);
if (Object.prototype.hasOwnProperty.call(basejieri, parHoliday)) {
  aData = basejieri[parHoliday];
}

var sFtv = {
  '0101': '元旦',
  '0214': '情人节',
  '0308': '妇女节',
  '0312': '植树节',
  '0401': '愚人节',
  '0501': '劳动节',
  '0504': '青年节',
  '0512': '护士节',
  '0601': '儿童节',
  '0701': '建党节',
  '0801': '建军节',
  '0910': '教师节',
  1001: '国庆节',
  1111: '光棍节',
  1224: '平安夜',
  1225: '圣诞节',
};

//农历节日
var lunarHoliday = {
  '0101': '春节',
  '0115': '元宵节',
  '0505': '端午节',
  '0707': '七夕情人节',
  '0715': '中元节',
  '0815': '中秋节',
  '0909': '重阳节',
  1208: '腊八节',
  1224: '小年',
};

//判断y年的农历中那个月是闰月,不是闰月返回0
function leapMonth(y: number) {
  var lm = lunarInfo[y - 1900] & 0xf;
  return lm == 0xf ? 0 : lm;
}

//返回农历y年m月的总天数
function monthDays(y: number, m: number) {
  return lunarInfo[y - 1900] & (0x10000 >> m) ? 30 : 29;
}

//返回农历y年闰月的天数
function leapDays(y: number) {
  if (leapMonth(y)) {
    return (lunarInfo[y - 1899] & 0xf) == 0xf ? 30 : 29;
  } else {
    return 0;
  }
}

//返回农历y年的总天数
function yearDays(y: number) {
  var i,
    sum = 348;
  for (i = 0x8000; i > 0x8; i >>= 1) {
    sum += lunarInfo[y - 1900] & i ? 1 : 0;
  }
  return sum + leapDays(y);
}

// 用中文显示农历的日期
function getChinaDay(d) {
  var s;

  switch (d) {
    case 10:
      s = '初十';
      break;
    case 20:
      s = '二十';
      break;
    case 30:
      s = '三十';
      break;
    default:
      s = chineseTen[Math.floor(d / 10)];
      s += chineseNumber[parseInt(String(d % 10))];
  }

  return s;
}
// calcute 农历年月日
function Lunar(objDate) {
  var i,
    leap = 0,
    temp = 0;
  var baseDate = new Date(1900, 0, 31);
  // objDate = (Date.UTC(objDate.getFullYear(), objDate.getMonth(), objDate.getDate())
  var offset = (objDate - baseDate) / 86400000;
  for (i = 1900; i < 2100 && offset > 0; i++) {
    temp = yearDays(i);
    offset -= temp;
  }
  if (offset < 0) {
    offset += temp;
    i--;
  }
  this.year = i;
  // 闰哪个月
  leap = leapMonth(i);
  this.isLeap = false;

  for (i = 1; i < 13 && offset > 0; i++) {
    // 闰月
    if (leap > 0 && i == leap + 1 && this.isLeap == false) {
      --i;
      this.isLeap = true;
      temp = leapDays(this.year);
    } else {
      temp = monthDays(this.year, i);
    }
    // 解除闰月
    if (this.isLeap == true && i == leap + 1) {
      this.isLeap = false;
    }

    offset -= temp;
  }

  if (offset == 0 && leap > 0 && i == leap + 1) {
    if (this.isLeap) {
      this.isLeap = false;
    } else {
      this.isLeap = true;
      --i;
    }
  }
  if (offset < 0) {
    offset += temp;
    --i;
  }

  this.month = i;
  this.day = offset + 1;
}

// 获取农历
function getLunarCalendar(year, month, day) {
  var lDate = new Lunar(new Date(year, month, day));
  var y = lDate.year;
  var m = lDate.month;
  var d = lDate.day;

  var lunar = '';
  var paraHoliday = (m > 9 ? m : '0' + m) + '' + (d > 9 ? d : '0' + d);

  // 农历节日
  if (Object.prototype.hasOwnProperty.call(lunarHoliday, paraHoliday)) {
    return lunarHoliday[paraHoliday];
  }

  if (m == 12) {
    var theLastDay = lDate.isLeap ? leapDays(y) : monthDays(y, m); // 农历当月最後一天
    if (theLastDay == d) {
      return '除夕';
    }
  }

  // 节气
  paraHoliday =
    (month + 1 > 9 ? month + 1 : '0' + (month + 1)) +
    '' +
    (day > 9 ? day : '0' + day);
  for (var i = 0; i < sTermDateArr.length; i++) {
    if (paraHoliday == sTermDateArr[i]) {
      return solarTerm[i];
    }
  }

  if (d == 1) {
    lunar = chineseNumber[m] + '月';
  } else {
    lunar = getChinaDay(d);
  }
  return lunar;
}

// 获取y年的节气
var sTermDateArr = [];
function sTermDate(y) {
  sTermDateArr = [];
  var date = null;
  var stdateStr = '';
  for (var i = 0; i < solarTerm.length; i++) {
    date = new Date(
      31556925974.7 * (y - 1900) +
        termInfo[i] * 60000 +
        Date.UTC(1900, 0, 6, 2, 5),
    );
    stdateStr =
      (date.getMonth() + 1 > 9
        ? date.getMonth() + 1
        : '0' + (date.getMonth() + 1)) +
      '' +
      (date.getUTCDate() > 9 ? date.getUTCDate() : '0' + date.getUTCDate());
    sTermDateArr.push(stdateStr);
  }
}

export default {};
